/**
 * Moondance UML modeling tool
 * 
 * This program is free software; you can redistribute it and
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package ro.pub.cs.se.moondance.editpart;

import java.beans.PropertyChangeEvent;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.ChopboxAnchor;
import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.NodeEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.requests.DirectEditRequest;
import org.eclipse.gef.tools.DirectEditManager;
import org.eclipse.jface.viewers.TextCellEditor;

import ro.pub.cs.se.moondance.directedit.ExtendedDirectEditManager;
import ro.pub.cs.se.moondance.directedit.LabelCellEditorLocator;
import ro.pub.cs.se.moondance.editor.figures.CompositeStateFigure;
import ro.pub.cs.se.moondance.editor.figures.EditableLabel;
import ro.pub.cs.se.moondance.editpolicy.CompositeStateDeleteEditPolicy;
import ro.pub.cs.se.moondance.editpolicy.CompositeStateDirectEditPolicy;
import ro.pub.cs.se.moondance.editpolicy.CompositeStateLayoutEditPolicy;
import ro.pub.cs.se.moondance.editpolicy.UMLNodeEditPolicy;
import ro.pub.cs.se.moondance.model.UMLCompositeState;
import ro.pub.cs.se.moondance.model.UMLEntity;

public class CompositeStateEditPart extends UMLEntityEditPart implements
		NodeEditPart {
	protected DirectEditManager manager;
	private ConnectionAnchor anchor;

	// ******************* Life-cycle related methods *********************/

	/**
	 * @see org.eclipse.gef.EditPart#activate()
	 */
	public void activate() {
		super.activate();
	}

	/**
	 * @see org.eclipse.gef.EditPart#deactivate()
	 */
	public void deactivate() {
		super.deactivate();
	}

	// ******************* Model related methods *********************/

	/**
	 * Returns the CompositeState model object represented by this EditPart
	 */
	public UMLCompositeState getCompositeStateModel() {
		return (UMLCompositeState) getModel();
	}

	/**
	 * @return the children Model objects as a new ArrayList
	 */
	protected List<UMLEntity> getModelChildren() {
		ArrayList<UMLEntity> list = new ArrayList<UMLEntity>(
				getCompositeStateModel().getStates());
		list.addAll(getCompositeStateModel().getRegions());
		return list;
	}

	// ******************* Editing related methods *********************/

	/**
	 * Creates edit policies and associates these with roles
	 */
	protected void createEditPolicies() {
		installEditPolicy(EditPolicy.LAYOUT_ROLE,
				new CompositeStateLayoutEditPolicy());
		installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE,
				new CompositeStateDirectEditPolicy());
		installEditPolicy(EditPolicy.COMPONENT_ROLE,
				new CompositeStateDeleteEditPolicy());
		installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE,
				new UMLNodeEditPolicy());

	}

	// ******************* Direct editing related methods *********************/

	/**
	 * @see org.eclipse.gef.EditPart#performRequest(org.eclipse.gef.Request)
	 */
	public void performRequest(Request request) {
		if (request.getType() == RequestConstants.REQ_DIRECT_EDIT) {
			if (request instanceof DirectEditRequest
					&& !directEditHitTest(((DirectEditRequest) request)
							.getLocation().getCopy()))
				return;
			performDirectEdit();
		}
	}

	private boolean directEditHitTest(Point requestLoc) {
		CompositeStateFigure figure = (CompositeStateFigure) getFigure();
		EditableLabel nameLabel = figure.getNameLabel();
		nameLabel.translateToRelative(requestLoc);
		if (nameLabel.containsPoint(requestLoc))
			return true;
		return false;
	}

	protected void performDirectEdit() {
		if (manager == null) {
			CompositeStateFigure figure = (CompositeStateFigure) getFigure();
			EditableLabel nameLabel = figure.getNameLabel();
			manager = new ExtendedDirectEditManager(this, TextCellEditor.class,
					new LabelCellEditorLocator(nameLabel), nameLabel, null);
		}
		manager.show();
	}

	/**
	 * @param handles
	 *            the name change during an edit
	 */
	public void handleNameChange(String value) {
		CompositeStateFigure stateFigure = (CompositeStateFigure) getFigure();
		EditableLabel label = stateFigure.getNameLabel();
		label.setVisible(false);
		refreshVisuals();
	}

	/**
	 * Reverts to existing name in model when exiting from a direct edit
	 * (possibly before a commit which will result in a change in the label
	 * value)
	 */
	public void revertNameChange() {
		CompositeStateFigure stateFigure = (CompositeStateFigure) getFigure();
		EditableLabel label = stateFigure.getNameLabel();
		UMLCompositeState _compositeState = getCompositeStateModel();
		label.setText(_compositeState.getLabelText());
		label.setVisible(true);
		refreshVisuals();
	}

	// ******************* Miscellaneous stuff *********************/

	/**
	 * @see org.eclipse.gef.editparts.AbstractEditPart#toString()
	 */
	public String toString() {
		return getModel().toString();
	}

	// ******************* Listener related methods *********************/

	/**
	 * Handles change in name when committing a direct edit
	 */
	protected void commitNameChange(PropertyChangeEvent evt) {
		CompositeStateFigure stateFigure = (CompositeStateFigure) getFigure();
		EditableLabel label = stateFigure.getNameLabel();
		label.setText(getCompositeStateModel().getLabelText());
		label.setVisible(true);
		refreshVisuals();
	}

	// ******************* Layout related methods *********************/

	public IFigure getContentPane() {
		CompositeStateFigure figure = (CompositeStateFigure) getFigure();
		return figure.getMainFigure();
	}

	protected ConnectionAnchor getConnectionAnchor() {
		if (anchor == null) {
			anchor = new ChopboxAnchor(getFigure());
		}
		return anchor;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.gef.NodeEditPart#getSourceConnectionAnchor(org.eclipse.gef
	 * .ConnectionEditPart)
	 */
	public ConnectionAnchor getSourceConnectionAnchor(
			ConnectionEditPart connection) {
		return getConnectionAnchor();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.gef.NodeEditPart#getSourceConnectionAnchor(org.eclipse.gef
	 * .Request)
	 */
	public ConnectionAnchor getSourceConnectionAnchor(Request request) {
		return getConnectionAnchor();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.gef.NodeEditPart#getTargetConnectionAnchor(org.eclipse.gef
	 * .ConnectionEditPart)
	 */
	public ConnectionAnchor getTargetConnectionAnchor(
			ConnectionEditPart connection) {
		return getConnectionAnchor();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.gef.NodeEditPart#getTargetConnectionAnchor(org.eclipse.gef
	 * .Request)
	 */
	public ConnectionAnchor getTargetConnectionAnchor(Request request) {
		return getConnectionAnchor();
	}

	/**
	 * Sets the width of the line when selected
	 */
	public void setSelected(int value) {
		super.setSelected(value);/*
								 * CompositeStateFigure stateFigure =
								 * (CompositeStateFigure) getFigure(); if (value
								 * != EditPart.SELECTED_NONE)
								 * stateFigure.setSelected(true); else
								 * stateFigure.setSelected(false);
								 * stateFigure.repaint();
								 */
	}

}
